import type { Component, Ref, computed } from 'vue'
import type { RouteMeta } from 'vue-router'
import type { voidfn } from './common'
import type { RouterHistory } from './history'
import type { RouteRecordNormalized } from './matcher'

// 用户传入的原始的路由记录类型
export interface RouteRecordRaw {
  path: string
  name?: string
  children?: RouteRecordRaw[]
  component: Component
  meta?: RouteMeta
  beforeEnter?: voidfn
}

export interface RouterOptions {
  history: RouterHistory
  routes: Readonly<RouteRecordRaw[]>
}

export interface Router {
  push(to:string | Ref<string> | RouteLocationNormalized): void
  install(app: App): void
  beforeEach(guard: voidfn): void
  beforeResolve(guard: voidfn): void
  afterEach(guard: voidfn): void
}

// App 类型
export interface App {
  push: (to: RouteLocationNormalized) => void
  beforeEach: (handler: unknown) => void
  afterEach: (handler: unknown) => void
  beforeResolve: (handler: unknown) => void
  install(): void
  config: {
    globalProperties: {
      $router: {}
    }
  }
  provide: (key: string, value: Router | ReactiveRoute) => void
  component: (name: string, component: Component) => void
}

export interface RouteLocationNormalized {
  path: string
  matched: RouteRecordNormalized[]
}

export type ReactiveRoute = Record<string, ReturnType<typeof computed>>

